package com.green.common.util;

import java.util.HashMap;
import java.util.Map;

import net.sourceforge.pinyin4j.PinyinHelper;

/**
 * @className:PinyingUtil.java
 * @classDescription:拼音操作工具类
 * @author:xiayingjie
 * @createTime:2010-10-21
 */

public class PinyinUtil {
	private static Map<String,String> realPin = new HashMap<String,String>();
    public static String getSpell(String target) {
        return getSpell(target, false);
    }

    public static String getSpell(String target, boolean isSimpleSpell) {
        char[] chars = target.toCharArray();
        StringBuffer spell = new StringBuffer();
        String[] pinyinStr;
        int endIndex;
        for (char ch : chars) {
            // 如果不是汉字
            if (ch <= 128) {
                spell.append(ch);
                continue;
            }
            pinyinStr = PinyinHelper.toHanyuPinyinStringArray(ch);
            // 过滤中文符号
            if (pinyinStr == null) {
                spell.append(ch);
                continue;
            }
            endIndex = isSimpleSpell ? 1 : pinyinStr[0].length() - 1;
            spell.append(pinyinStr[0].substring(0, endIndex));
        }
        return spell.toString();
    }
    
    public static String[] getSpellToArray(String target){
        char[] chars = target.toCharArray();
        String[] spell = new String[chars.length];
        String[] pinyinStr;
        for (int i = 0; i < chars.length; i++) {
            // 如果不是汉字
            if (chars[i] <= 128) {
                spell[i] = String.valueOf(chars[i]);
                continue;
            }
            pinyinStr = PinyinHelper.toHanyuPinyinStringArray(chars[i]);
            // 过滤中文符号
            if (pinyinStr == null) {
                spell[i] = String.valueOf(chars[i]);
                continue;
            }
            spell[i] = pinyinStr[0].substring(0, pinyinStr[0].length() - 1);
        }
        return spell;
    }
    public static String getRealSimplePin(String name){
    	realPin.put("分行", "FH");
    	realPin.put("一行", "YH");
    	realPin.put("二行", "EH");
    	realPin.put("三行", "SH");
    	realPin.put("四行", "SH");
    	realPin.put("五行", "WH");
    	realPin.put("六行", "LH");
    	realPin.put("七行", "QH");
    	realPin.put("八行", "BH");
    	if(StringUtil.isEmpty(name)){
    		return "";
    	}
    	if(name.length()<2){
    		return PinyinUtil.getSpell(name, true).toUpperCase();
    	}
    	String pin = name.substring(name.length()-2, name.length()); //后两个字
		if(realPin.containsKey(pin)){
			name = name.replaceAll(pin, realPin.get(pin));
		}
    	return PinyinUtil.getSpell(name, true).toUpperCase();
    }
    public static void main(String[] args) {
    	System.out.println(PinyinUtil.getRealSimplePin("苏州"));
    	System.out.println(PinyinUtil.getRealSimplePin("深圳"));
    	System.out.println(PinyinUtil.getRealSimplePin("宿州"));
//    	System.out.println(PinyinUtil.getRealSimplePin("分行"));
//    	System.out.println(PinyinUtil.getRealSimplePin("一行"));
//    	System.out.println(PinyinUtil.getRealSimplePin("诺德二行"));
//    	System.out.println(PinyinUtil.getRealSimplePin("二行诺德"));
    }

}